package com.uyong.study.snake;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;

public class GameFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	private GamePanel gPanel;
	private JLabel jLabel;
	private JMenu[] jMenus;
	private JMenuBar jBar;
	private JMenuItem[] jItems;

	public GameFrame() {
		super("贪吃蛇游戏");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
		setSize(SizeConstants.WIDTH * SizeConstants.SIZE + 20, SizeConstants.HEIGHT * SizeConstants.SIZE + 90);
		setLayout(null);

		jLabel = new JLabel();
		jLabel.setBorder(BorderFactory.createEtchedBorder(Color.BLUE, Color.GRAY));
		jLabel.setBounds(1, 1, getWidth() - 8, 25);
		add(jLabel);

		jMenus = new JMenu[4];
		jItems = new JMenuItem[9];
		jBar = new JMenuBar();

		jMenus[0] = new JMenu("菜单", true);
		jMenus[1] = new JMenu("排名");
		jMenus[2] = new JMenu("帮助");
		jMenus[3] = new JMenu("难度选择");

		jItems[0] = new JMenuItem("开始游戏");
		jItems[0].setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_ENTER));
		jItems[1] = new JMenuItem("暂停\\开始");
		jItems[1].setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_SPACE));
		jItems[2] = new JMenuItem("重新开始");
		jItems[2].setAccelerator(KeyStroke.getKeyStroke('A', (char) KeyEvent.VK_A));
		jItems[3] = new JRadioButtonMenuItem("容易");
		jItems[3].setAccelerator(KeyStroke.getKeyStroke("F1"));
		jItems[4] = new JRadioButtonMenuItem("中等");
		jItems[4].setAccelerator(KeyStroke.getKeyStroke("F2"));
		jItems[5] = new JRadioButtonMenuItem("困难");
		jItems[5].setAccelerator(KeyStroke.getKeyStroke("F3"));
		jItems[6] = new JMenuItem("排行榜");//
		jItems[7] = new JMenuItem("游戏帮助");
		jItems[8] = new JMenuItem("关于游戏");

		ButtonGroup group = new ButtonGroup();
		for (int i = 0; i < 3; i++) {
			jBar.add(jMenus[i]);
			group.add(jItems[3 + i]);
		}

		for (int i = 0; i < 3; i++) {
			jMenus[0].add(jItems[i]);
			jMenus[3].add(jItems[3 + i]);
			jMenus[0].addSeparator();
			jMenus[3].addSeparator();
		}
		jMenus[0].add(jMenus[3]);
		jMenus[1].add(jItems[6]);
		jMenus[2].add(jItems[7]);
		jMenus[2].add(jItems[8]);

		for (int i = 0; i < 9; i++) {
			jItems[i].addActionListener(new MyAction());
		}

		setJMenuBar(jBar);

		gPanel = new GamePanel();
		gPanel.setLocation(6, 32);
		add(gPanel);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		setVisible(true);
		gPanel.requestFocus();
		showScore();
	}

	// 显示得分
	public void showScore() {
		while (true) {
			jLabel.setText("  你的得分：" + gPanel.getScore() + "       蛇的长度：" + gPanel.getLength() + "     当前速度："
				+ (300 - gPanel.getSpeed()));
		}
	}

	private class MyAction implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			String s = e.getActionCommand();
			if (s == "开始游戏") {
				gPanel.startGame();
			}
			if (s == "暂停\\开始") {
				gPanel.stop();
			}
			if (s == "容易") {
				gPanel.setSpeed(200);
			}
			if (s == "中等") {
				gPanel.setSpeed(150);
			}
			if (s == "困难") {
				gPanel.setSpeed(100);
			}
			if (s == "游戏帮助") {}
		}
	}

	public static void main(String[] args) {
		new GameFrame();
	}

}
